package org.lanqiao.ssm.common.web.controller;

import java.io.Serializable;

import org.lanqiao.ssm.common.facede.BaseManagerFacedeI;
import org.lanqiao.ssm.common.facede.BaseQueryFacedeI;
import org.lanqiao.ssm.common.model.Json;
import org.lanqiao.ssm.common.web.controller.base.BasicController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiParam;


/**
 * 
 * @ClassName: BaseFormController
 * @Description:(控制类)
 * @author 刘伟
 * @date 2015-11-9 下午2:21:45
 * 
 */
@RestController
public abstract class BaseController<M, ID extends Serializable> extends BasicController
		implements BaseControllerI<M, ID> {
	public abstract BaseManagerFacedeI<M, ID> getManagerFacede();

	public abstract BaseQueryFacedeI<M, ID> getQueryFacede();

	@RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.POST)
	public Json deleteByPrimaryKey(@ApiParam(value = "id") @RequestParam(required = true) ID id) throws Exception {

		return getManagerFacede().deleteByPrimaryKey(id) > 0 ? setSimpleSuccess(id) : setFailed();
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Json insert(M record) throws Exception {

		return getManagerFacede().insert(record) > 0 ? setSimpleSuccess(record) : setFailed();
	}

	@RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
	public Json insertSelective(M record) throws Exception {

		return getManagerFacede().insertSelective(record) > 0 ? setSimpleSuccess(record) : setFailed();
	}

	@RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
	public M selectByPrimaryKey(@ApiParam(value = "id") @RequestParam(value = "id", required = true) ID id)
			throws Exception {
		return getQueryFacede().selectByPrimaryKey(id);
	}

	@RequestMapping(value = "/updateByPrimaryKeySelective", method = RequestMethod.POST)
	public Json updateByPrimaryKeySelective(M record) throws Exception {

		return getManagerFacede().updateByPrimaryKeySelective(record) > 0 ? setSimpleSuccess(record) : setFailed();
	}

	@RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
	public Json updateByPrimaryKey(M record) throws Exception {

		return getManagerFacede().updateByPrimaryKey(record) > 0 ? setSimpleSuccess(record) : setFailed();
	}

	@RequestMapping(value = "/findObjectsByPage", method = RequestMethod.GET)
	public PageInfo<M> findObjectsByPage(M record, int page, int pageShow) throws Exception {

		return getQueryFacede().findObjectsByPage(record, page, pageShow);
	}

}
